<?php

namespace app\api\controller\permission;

use app\api\controller\common\ApiBase;
use think\Db;

class Admincompany extends ApiBase
{
    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName="";
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    /**
     * 查看角色
     *
     * @return void
     */
    public function index()
    {
        if (request()->isPost()) {
            $exhibiton_id =$this->request->param('exhibiton_id');
            $page = $this->request->param('page');
            $keyword = $this->request->param('keyword');
            $page = max(1,intval($page));
            $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
            $tabName=$this->getTabName($exhibiton_id);
            $param='';
            if (!empty($keyword)) {
                $param.=" and a.realname like '%$keyword%'";
            }
            $sql2="SELECT a.realname,COUNT(b.id) AS instnum FROM crm_admin a LEFT JOIN crm_".$tabName." b ON a.id = b.member_id ";
            $sql2.=" where 1=1 $param GROUP BY a.id ";
            $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
            $list=Db::query($sql2);
            $sql21=" select count(a.id) as count1 ";
            $sql21.=" from crm_admin as a where 1=1 $param ";
            $total1=Db::query($sql21);
            $total=$total1[0]['count1']; //st
            return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'total'=>$total]]);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    
}
